const baseUrl = "http://47.113.124.237:8833/api";

export const api = {
    /**
     * 获取网站信息
     */
    getWebsiteInformation: async function () {
        try {
            const response = await fetch(`${baseUrl}/user/findWebsiteInformation`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error fetching website information:", error);
        }
    },

    /**
     * 获取网站轮播图
     */
    getCarousel: async function () {
        try {
            const response = await fetch(`${baseUrl}/user/findCarousel`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error fetching carousel:", error);
        }
    },

    /**
     * 根据导航名搜索分类列表
     * @param navigationName 导航名
     */
    getNewsTypeList: async function (navigationName: string) {
        try {
            const response = await fetch(`${baseUrl}/user/findNewsTypeList?type=${encodeURIComponent(navigationName)}`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error(`Error fetching news type list for ${navigationName}:`, error);
        }
    },

    /**
     * 根据分类查询所有新闻
     * @param categoryName 分类名
     * @param page 页数
     * @param size 每页条数
     */
    getNewsCenterPage: async function (categoryName: string, page: number, size: number) {
        try {
            const response = await fetch(`${baseUrl}/user/findNewsCenterPage?type2=${encodeURIComponent(categoryName)}&page=${page}&size=${size}`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error(`Error fetching news for category ${categoryName}:`, error);
        }
    },

    /**
     * 根据文章id查询文章详情
     * @param newsId 文章id
     */
    getNewsCenterById: async function (newsId: number) {
        try {
            const response = await fetch(`${baseUrl}/user/findNewsCenter?id=${newsId}`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error(`Error fetching news details for id ${newsId}:`, error);
        }
    },

    /**
     * 根据路径获取文件资源
     * @param resourcePath 文件路径
     */
    getResourceByPath: async function (resourcePath: string) {
        try {
            const response = await fetch(`${baseUrl}/file/${resourcePath}`);
            if (response.status === 200 && response.url.length > 0) {
                return response.url;
            } else return '';
        } catch (error) {
            console.error(`Error fetching image for path ${resourcePath}:`, error);
        }
    },

    // 留言板
    /**
     * 提交留言
     * @param formData 表单数据
     */
    postMessage: async function (formData: { name: string; phoneNumber: string; mailbox: string; msg: string }) {
        try {
            const response = await fetch(`${baseUrl}/user/addMessage`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(formData),
            });
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error posting message:", error);
        }
    },

    /**
     * 获取留言
     * @param page 页数
     * @param size 每页条数
     */
    getMessage: async function (page: number, size: number) {
        try {
            const response = await fetch(`${baseUrl}/user/findMessagePage?page=${page}&size=${size}`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error fetching message:", error);
        }
    },

    /**
     * 公式专区
     * 获取TreeSelect渲染数据
     */
    getTreeStructure: async function () {
        try {
            const response = await fetch(`${baseUrl}/user/findSchool`);
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error fetching tree structure:", error);
        }
    },

    /**
     * 公式专区
     * 根据TreeSelect筛选得出的筛选条件数组获取对应的图片列表
     * @param filter 筛选条件数组
     */
    getTreeImageList: async function (filter: string[]) {
        try {
            const response = await fetch(`${baseUrl}/user/findSchoolPicList?`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(filter)
            });
            const data = await response.json();
            return data;
        } catch (error) {
            console.error("Error fetching tree image list:", error);
        }
    }
};
